﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using DoAn.DTO;

namespace DoAn.DAO
{
    public class DanhMucDAO
    {
        public static DataTable loadDanhMuc()
        {
            string sql="select * from DANHMUC";
            return DataProvider.DataTable(sql);
        }
        public static DataTable loadDanhMucCha()
        {
            string sql = "select * from DANHMUC where MaDanhMucCha=0";
            return DataProvider.DataTable(sql);
        }

        public static int  layMaDanhMucCha(DanhMucDTO dm)
        {
            string sql="select * from DanhMuc where TenDanhMuc=N'"+dm.TenDanhMuc+"'";
            DataTable dt = new DataTable();
            dt = DataProvider.DataTable(sql);
            return int.Parse(dt.Rows[0]["MaDanhMuc"].ToString());
        }
        public static int layMaDanhMucCon(DanhMucDTO dm)
        {
            string sql = "select * from DanhMuc where TenDanhMuc=N'"+dm.TenDanhMuc+"' and MaDanhMucCha="+dm.MaDanhMucCha.ToString()+"";
            DataTable dt = new DataTable();
            dt = DataProvider.DataTable(sql);
            return int.Parse(dt.Rows[0]["MaDanhMuc"].ToString());
        }
        public static string layTenDanhMuc(DanhMucDTO dm)
        {
            string sql = "Select * from DanhMuc where MaDanhMuc="+ dm.MaDanhMuc.ToString();
            DataTable dt = new DataTable();
            dt = DataProvider.DataTable(sql);
            return dt.Rows[0]["TenDanhMuc"].ToString();
        }
        public static List<int> arrmangdanhmuccon(int madmcha)
        {
            string sql = "select * from DanhMuc where MaDanhMucCha='"+madmcha.ToString()+"'";
            DataTable dt = new DataTable();
            dt = DataProvider.DataTable(sql);
            List<int> list = new List<int>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Convert.ToInt32(row["MaDanhMuc"].ToString()));
            }
            return list;
        }
        
        
    }
}